class DateUtil {
  static DateTime getDateTime(String dateStr) {
    DateTime? dateTime = DateTime.tryParse(dateStr);
    if (dateTime == null) {
      return DateTime.now();
    }
    return dateTime;
  }

  static DateTime getDateTimeByMs(int milliseconds, {bool isUtc = false}) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds, isUtc: isUtc);
    return dateTime;
  }

  static int getDateMsByTimeStr(String dateStr) {
    DateTime? dateTime = DateTime.tryParse(dateStr);
    return dateTime == null ? 0 : dateTime.millisecondsSinceEpoch;
  }

  static int getNowDateMs() {
    return DateTime.now().millisecondsSinceEpoch;
  }

  ///date Time Separate.
  static String dateTimeSeparate(String time, String? dateSeparate, String? timeSeparate) {
    if (dateSeparate != null) {
      time = time.replaceAll("-", dateSeparate);
    }
    if (timeSeparate != null) {
      time = time.replaceAll(":", timeSeparate);
    }
    return time;
  }

  ///get WeekDay By Milliseconds.
  static String getWeekDayByMs(int milliseconds, {bool isUtc = false}) {
    DateTime dateTime = getDateTimeByMs(milliseconds, isUtc: isUtc);
    return getWeekDay(dateTime);
  }

  ///get ZH WeekDay By Milliseconds.
  static String getZHWeekDayByMs(int milliseconds, {bool isUtc = false}) {
    DateTime dateTime = getDateTimeByMs(milliseconds, isUtc: isUtc);
    return getZHWeekDay(dateTime);
  }

  ///get WeekDay.
  static String getWeekDay(DateTime dateTime) {
    String weekday;
    switch (dateTime.weekday) {
      case 1:
        weekday = "Monday";
        break;
      case 2:
        weekday = "Tuesday";
        break;
      case 3:
        weekday = "Wednesday";
        break;
      case 4:
        weekday = "Thursday";
        break;
      case 5:
        weekday = "Friday";
        break;
      case 6:
        weekday = "Saturday";
        break;
      case 7:
        weekday = "Sunday";
        break;
      default:
        weekday = "Monday";
        break;
    }
    return weekday;
  }

  ///get ZH WeekDay.
  static String getZHWeekDay(DateTime dateTime) {
    String weekday;
    switch (dateTime.weekday) {
      case 1:
        weekday = "星期一";
        break;
      case 2:
        weekday = "星期二";
        break;
      case 3:
        weekday = "星期三";
        break;
      case 4:
        weekday = "星期四";
        break;
      case 5:
        weekday = "星期五";
        break;
      case 6:
        weekday = "星期六";
        break;
      case 7:
        weekday = "星期日";
        break;
      default:
        weekday = "星期一";
        break;
    }
    return weekday;
  }

  ///是否是闰年
  static bool isLeapYearByDateTime(DateTime dateTime) {
    return isLeapYearByYear(dateTime.year);
  }

  ///是否是闰年
  static bool isLeapYearByYear(int year) {
    return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
  }

  ///是否相等.
  static bool isSameDay(DateTime startTime, DateTime endTime) {
    return startTime.compareTo(endTime) == 0;
  }

  ///获取现在的年份
  static int getCurrentYear() {
    return DateTime.now().year;
  }

  ///获取现在的月份
  static int getCurrentMonth() {
    return DateTime.now().month;
  }

  ///获取现在的日
  static int getCurrentDay() {
    return DateTime.now().day;
  }

  static String getNowTimeStr() {
    String twoStr(int value) {
      if (value >= 10) {
        return value.toString();
      }
      return '0$value';
    }

    DateTime now = DateTime.now();

    int year = now.year;
    int month = now.month;
    int day = now.day;
    int hour = now.hour;
    int minute = now.minute;
    int second = now.second;

    return '$year-${twoStr(month)}-${twoStr(day)} ${twoStr(hour)}:${twoStr(minute)}:${twoStr(second)}';
  }
}
